package com.first.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.EduCourse;
import com.first.common.pojo.TbZoneCourse;
import com.first.common.vo.Result;
import com.first.system.dao.TbZoneCourseDao;
import com.first.system.dao.ZoneCourseDao;
import com.first.system.service.ZoneCourseService;
import com.first.system.vo.ZoneCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-05-29 18:40
 **/
@Service
public class ZoneCourseServiceImpl implements ZoneCourseService {
    @Autowired
    private ZoneCourseDao zoneCourseDao;
    @Autowired
    private TbZoneCourseDao tbZoneCourseDao;

    @Override
    public Result findZoneCourse(Integer currentPage, Integer pageSize, ZoneCourseVo zoneCourseVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("tzc.is_deleted",0);
        wrapper.eq("tz.id",zoneCourseVo.getZoneId());
        if (StringUtils.hasText(zoneCourseVo.getCourseTitle())) {
            wrapper.like("ec.title", zoneCourseVo.getCourseTitle());
        }
        if(StringUtils.hasText(zoneCourseVo.getIsDisable())){
            wrapper.eq("tzc.is_disable",zoneCourseVo.getIsDisable());
        }
        Page<EduCourse> page = new Page<>(currentPage,pageSize);
        IPage<EduCourse> allCourse = zoneCourseDao.findAllCourse(page, wrapper);
        return Result.success(allCourse);
    }

    @Override
    public Result findCourse(Integer currentPage, Integer pageSize, ZoneCourseVo zoneCourseVo) {
        //先查出来专区里已经上传的课程
        QueryWrapper<TbZoneCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("zone_id",zoneCourseVo.getZoneId());
        wrapper.eq("is_deleted",0);
        List<TbZoneCourse> tbZoneCourses = tbZoneCourseDao.selectList(wrapper);
        //取出他们的课程id
        List<String> collect = tbZoneCourses.stream().map(item -> item.getCourseId()).collect(Collectors.toList());

        //查出所有的课程
        QueryWrapper<EduCourse> wrapper1 = new QueryWrapper<>();
        if (StringUtils.hasText(zoneCourseVo.getTitle())) {
            wrapper1.like("ec.title", zoneCourseVo.getTitle());
        }
        if(StringUtils.hasText(zoneCourseVo.getStatus())){
            wrapper1.eq("ec.status",zoneCourseVo.getStatus());
        }
        wrapper1.eq("ec.is_deleted",0);
        Page<EduCourse> page = new Page<>(currentPage, pageSize);
        IPage<EduCourse> allCourse1 =zoneCourseDao.findAllZoneCourse(page,wrapper1);

        //查出来所有课程中没有添加的课程名
        List<EduCourse> collect1 = allCourse1.getRecords().stream().filter(item -> collect.stream().noneMatch(id -> Objects.equals(item.getId(), id)))
                .collect(Collectors.toList());
        //现将所有的都设置为已添加
        for (EduCourse eduCourse : allCourse1.getRecords()) {
            eduCourse.setIsAdd(1);
        }
        //再将没有添加的课程设置为未添加
        for (EduCourse eduCourse : collect1) {
            eduCourse.setIsAdd(0);
        }
        return Result.success(allCourse1);

    }


    @Override
    public Result addCourseDetail(TbZoneCourse tbZoneCourse) {
        TbZoneCourse tbZoneCourse1 = new TbZoneCourse(IdUtil.getSnowflakeNextId() + "", LocalDateTime.now(), LocalDateTime.now(), 0, 0, tbZoneCourse.getZoneId(), tbZoneCourse.getCourseId());
        int insert = tbZoneCourseDao.insert(tbZoneCourse1);
        if(insert!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result changeStatus(String tzcid, Integer tzcisDisable) {
        TbZoneCourse tbZoneCourse = new TbZoneCourse();
        tbZoneCourse.setId(tzcid);
        tbZoneCourse.setIsDisable(tzcisDisable);
        int i = tbZoneCourseDao.updateById(tbZoneCourse);
        if(i!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result deleteZoneCourse(EduCourse eduCourse) {
        TbZoneCourse tbZoneCourse = new TbZoneCourse();
        tbZoneCourse.setId(eduCourse.getTzcid());
        tbZoneCourse.setIsDeleted(1);
        int i = tbZoneCourseDao.updateById(tbZoneCourse);
        if(i!=0){
            return Result.success();
        }
        return null;
    }


}
